﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessComponents.Managers ;
using BusinessComponents;

namespace WaterfrontLodge.WebForms.RoomTypeMgt
{
    public partial class RoomTypeAddEdit1 : System.Web.UI.Page
    {
        RoomTypeNavigationController ctrNavigation;
        IRoomTypeMgr typeMgr = ManagerFactory.RoomTypeMgr;
        protected void Page_Load(object sender, EventArgs e)
        {
            ctrNavigation = new RoomTypeNavigationController();
            ctrNavigation.PageLoad();
            ctrNavigation.CurrentPage = "RoomTypeAddEdit1.aspx";
            if (!IsPostBack)
            {
                if (ctrNavigation.RoomTypeDetails.TypeCode > 0)
                {
                    RoomType roomType = typeMgr.GetRoomType(ctrNavigation.RoomTypeDetails.TypeCode);
                    if (roomType != null)
                    {
                        if (roomType.ImagePath != null && roomType.ImagePath != string.Empty)
                        {
                            imgPicture.ImageUrl = roomType.ImagePath;
                            ctrNavigation.RoomTypeDetails.ImagePath = roomType.ImagePath;
                        }
                        else
                        {
                            imgPicture.ImageUrl = "~/Image/emptyroom.jpg";
                        }
                    }
                }
            }  
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!uploadImg.HasFile)
            {
                lblStatus.Text = "Plase select Upload file first.";
            }
            else
            {
                lblStatus.Text = "";
                if (uploadImg.FileName != string.Empty)
                {
                    //check file type & size
                    string fileName = uploadImg.FileName;

                    if (typeMgr.IsValidExtension(fileName))
                    {
                        if (typeMgr.IsValidFileSize(uploadImg.FileBytes.Length))
                        {
                            ctrNavigation.RoomTypeDetails.ImagePath = "~/Image/" + uploadImg.FileName;
                            uploadImg.SaveAs(Server.MapPath("~/Image/") + uploadImg.FileName);
                            imgPicture.ImageUrl = "~/Image/" + uploadImg.FileName;
                        }
                        else
                        {
                            lblStatus.Text = "Please upload Image file Less than 100 KB.";
                        }
                    }
                    else
                    {
                        lblStatus.Text = "Please upload jpg, jpeg, gif or png file only.";

                    }
                }
                else
                    ctrNavigation.RoomTypeDetails.ImagePath = string.Empty;
                
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ctrNavigation.RoomTypeDetails.TypeCode > 0)
            {

                RoomType roomType = typeMgr.GetRoomType(ctrNavigation.RoomTypeDetails.TypeCode);

                if (roomType != null)
                {
                    roomType.Description = ctrNavigation.RoomTypeDetails.Description;
                    roomType.RoomSize = ctrNavigation.RoomTypeDetails.RoomSize;
                    roomType.StdPrice = ctrNavigation.RoomTypeDetails.StdPrice;
                    roomType.MaxOccupant = ctrNavigation.RoomTypeDetails.MaxOccupant;
                    roomType.Remarks = ctrNavigation.RoomTypeDetails.Remarks;
                    roomType.ImagePath = ctrNavigation.RoomTypeDetails.ImagePath;
                    // lblStatus.Text = "Room information updated Successfully!";
                }
                else
                {
                    lblStatus.Text = "The room type can not be found!";
                }

            }
            else
            {
                bool retRoomType = typeMgr.CheckDuplicateRoomType(ctrNavigation.RoomTypeDetails.Description);
                if (retRoomType)
                {
                    lblStatus.Text = "Error! The Room Type Description already exists!";
                }
                else
                {
                    RoomType roomType = new RoomType();
                    roomType.Description = ctrNavigation.RoomTypeDetails.Description;
                    roomType.RoomSize = ctrNavigation.RoomTypeDetails.RoomSize;
                    roomType.StdPrice = ctrNavigation.RoomTypeDetails.StdPrice;
                    roomType.MaxOccupant = ctrNavigation.RoomTypeDetails.MaxOccupant;
                    roomType.Remarks = ctrNavigation.RoomTypeDetails.Remarks;
                    roomType.ImagePath = ctrNavigation.RoomTypeDetails.ImagePath;
                    typeMgr.Add(roomType);
                }
            }
            typeMgr.Save();

            Response.Redirect(ctrNavigation.NextPage());
        }
    }
}